#!/bin/env python
from ctypes import *
import sys,os

class sV3:
    def __init__(self,filename):
        if filename != None:
            self._fp = open(filename,'r')

    def open(self,filename):
        if self._fp != None:
            self._fp.close()
        self._fp = open(filename,'r')

    def close(self):
        self._fp.close()

    def g(self):
        doc={}
        self._fields=[]
        line = self._fp.readline()
        if line != '<doc>\x01\n':
            return None
        while True:
            line = self._fp.readline()
            if line == '</doc>\x01\n':
                break 
            pos=line.find('=')
            field=line[0:pos]
            val=''
            self._fields.append(field)
            if line[-2] == '\x01':
                val=line[pos+1:-2]
            else:
                val=line[pos+1:]
                while '\x01\n' not in line:
                    line = self._fp.readline()
                    val += line
                val=val[0:-2]
            
            doc[field]=val #;print field, val
        return doc
            
    def p(self,doc):
        print "<doc>\x01"
        if len(self._fields) == len(doc):
            for field in self._fields:
                print "%s=%s\x01" %(field,doc[field])
        else:
            for field in doc:
                print "%s=%s\x01" %(field,doc[field])
        print "</doc>\x01"

class sPB:
    def __init__(self,pb):
        self._pb = pb

class sQuery:
    def __init__(self,query):
        self.dic = {}
        self._q = query
        clauses = self._q[20:].split("&")
        for clause in clauses:
            key = clause.split('=')[0]
            value = clause.split('=')[1]
            sub_clauses = value.split(';')
            if len(sub_clauses) > 1:
                value = {}
                for sub_clause in sub_caluses:
                    

            self.dic[key] = value

    def printf(self):
        for i in self.dic:
            print i,"=",self.dic[i]
            
            
        
